package com.ifnk.portaljava.hlyt.etl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;

/**
 * <p>Title:插入目录里所有数据的类 </p>
 *
 * <p>Description: 将workDir目录里的所有数据文件插入数据库中</p>
 *
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * <p>Company: </p>
 * 
 * @author chengzhang.shi.hlyt@gmail.com
 * @version 1.0
 */
public class InsertDataMultiThread implements Runnable {

	private Log loger = LogFactory.getLog(InsertDataMultiThread.class);
	
	private File workDir;
	private File backupDir;
	
	public InsertDataMultiThread() {
		
	}

	public void run() {
		insertAllFile(workDir);
		moveAllFile(workDir, backupDir);
		workDir.delete();
	}

	private void copyFile(String source, String destination) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(new File(source));
			fos = new FileOutputStream(new File(destination));
			byte[] buff = new byte[1024];
			int readed = -1;
			while ((readed = fis.read(buff)) != -1) {
				fos.write(buff, 0, readed);
			}
			loger.info("文件" + source + "拷贝到" + destination + "完成。");
		} catch (FileNotFoundException e) {
			loger.error("文件" + source + "拷贝到" + destination + "出错！" + e);
			e.printStackTrace();
		} catch (IOException e) {
			loger.error("文件" + source + "拷贝到" + destination + "出错！" + e);
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void deleteFile(String file) {
		boolean isDeleted = new File(file).delete();
		if (isDeleted) {
			loger.info("删除文件" + file + "成功。");
		} else {
			loger.error("删除文件" + file + "失败！");
		}
	}
	
	private int moveAllFile(File source, File destination) {
		int num = 0;
		if(source.isDirectory() && destination.isDirectory()) {
			String[] dataFileNames = source.list();
			File[] dataFiles = source.listFiles();
			for (int i = 0; i < dataFileNames.length; i++) {
				if (dataFiles[i].isDirectory()) {
					continue;
				}
				copyFile(source.getPath() + File.separator + dataFileNames[i], 
						destination.getPath() + File.separator + dataFileNames[i]);
				deleteFile(source.getPath() + File.separator + dataFileNames[i]);
				num++;
			}
		} else {
			loger.error("moveAllFile:" + source.getPath() + "或者" + destination.getPath() + "不是目录！");
		}
		return num;
	}
	
	private void insertAllFile(File dir) {
		if(dir.isDirectory()) {
			File[] dataFiles = workDir.listFiles();
			if (dataFiles.length != 0) {
				for (int i = 0; i < dataFiles.length; i++) {
					GnFile gnFile = new GnFile();
					boolean flag = gnFile.setFile(dataFiles[i]);
					if(flag) {
						gnFile.insert();
					}
				}
			}
		}
	}
	
	public File getWorkDir() {
		return workDir;
	}

	public void setWorkDir(File workDir) {
		this.workDir = workDir;
	}
	
	public void setBackupDir(File backupDir) {
		this.backupDir = backupDir;
	}

}
